const app = getApp();
import Dialog from "../../../../../dist/dialog/dialog";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reason:"",
    apply_status:null,
    can_apply:0,
    role: 1,
    addressShow: false,
    streetShow: false,
    id_card_taill: [], //身份证反面
    id_card_front: [], //身份证正面


    districtVal: [], //街道数据

    province_id: "",
    city_id: "",
    district_id: "",
    street_id: "",


    openDateLw: false,
    selectAddressData: [],
    isShowStreet: false,
    pickerValue: [0, 0, 0],
    provinceData: [],
    cityData: [],
    districtData: [],


    form: {
      province: "",
      city: "",
      district: "",
      streetName: "",
      locationTitle: "",
      point: "",
    },

    realname: "",
    mobile: "",
    age: "",
    id_card_front_img: "",
    id_card_reverse_img: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.role) {
      this.setData({
        role: options.role
      })
      
      wx.setNavigationBarTitle({
        title: '发布者信息',
      })
    }

    //加载地址数据初始化
    this._initAddressInfo();
  },
  gotoRecord(){
    wx.navigateTo({
      url: '/packageJ/anytime_service/my/record/record?name=promulgator',
    })
  },
  validate(){
    app._getNetWork({
      url:app.getNetAddresss('plugin.be-within-call.frontend.role-apply.detail'),
      data:{
        role:1,
        validate_page:1
      },
      success:resdata=>{
        let {result,data} = resdata.data;
        if(result){
          this.setData({
            apply_status:data.apply_status,
            can_apply:data.can_apply,
            reason:data.reason
          })
            this.setData({
              isShowStreet:true,
              province_id:data.province_id,
              city_id:data.city_id,
              district_id:data.district_id,
              street_id:data.street_id,
              id_card_reverse_img:data.annex_file.id_card_reverse,
              id_card_front_img:data.annex_file.id_card_front,
              realname:data.realname,
              mobile:data.mobile,
              age:data.detail_info.age,
              mobile:data.mobile,
              addressName:`${data.province_name} ${data.city_name} ${data.district_name}`,
              "form.streetName":data.street_name,
              id_card_taill:[{url:data.annex_file.id_card_reverse_url}],
              id_card_front:[{url:data.annex_file.id_card_front_url}],
            })
        }
      }
    })
  },
  //选择街道
  streetConfirm(e) {
    let strname = e.target.dataset.strname;
    let street_id = e.target.dataset.id;
    this.setData({
      "form.streetName": strname,
      streetShow: false,
      street_id
    });
  },
  //打开地址选择器
  openDateLwtbtn: function () {
      this.setData({
        openDateLw: true,
      });
  },
  streetChoose() {
      this.setData({
        streetShow: true,
      });
  },
  // 收货地址滚动事件
  bindChange: function (e) {
    var val = e.detail.value;
    if (this.data.pickerValue[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
    }
    if (this.data.pickerValue[1] != val[1]) {
      val[2] = 0;
    }
    this.setData({
      "pickerValue[0]": val[0],
      "pickerValue[1]": val[1],
      "pickerValue[2]": val[2],
    });
    this._initAddressInfo();
  },
  //地址选择器数据初始化
  _initAddressInfo: function () {
    var that = this;
    var _keyName = "provinceData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        that._getProvinceData();
      },
      fail: function (res) {
        app._getNetWork({
          url: app.getNetAddresss("member.member-address.address"),
          success: function (res) {
            var response = res.data;
            var province = response.data.province;
            var city = response.data.city;
            var district = response.data.district;
            wx.setStorage({
              key: "provinceData",
              data: province,
            });
            wx.setStorage({
              key: "cityData",
              data: city,
            });
            wx.setStorage({
              key: "districtData",
              data: district,
            });
            that._initAddressInfo();
          },
          fail: function (res) {},
          state: true,
        });
      },
    });
  },
  // 读取省key
  _getProvinceData: function () {
    var that = this;
    var _keyName = "provinceData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        that.setData({
          provinceData: res.data,
        });
        that.setData({
          "selectAddressData[0].name": res.data[that.data.pickerValue[0]].areaname,
          "selectAddressData[0].id": res.data[that.data.pickerValue[0]].id,
          "selectAddressData[0].index": that.data.pickerValue[0],
        });
        that._getCityData();
      },
    });
  },
  // 读取城市key，筛选城市data
  _getCityData: function () {
    var _data = this.data.selectAddressData[0];
    var that = this;
    var _keyName = "cityData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        that.setData({
          cityData: _json,
        });
        that.setData({
          "selectAddressData[1].name": _json[that.data.pickerValue[1]].areaname,
          "selectAddressData[1].id": _json[that.data.pickerValue[1]].id,
          "selectAddressData[1].index": that.data.pickerValue[1],
        });
        that._getDistrictData();
      },
    });
  },
  // 读取地区key，筛选地区data
  _getDistrictData: function () {
    var _data = this.data.selectAddressData[1];
    var that = this;
    var _keyName = "districtData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        that.setData({
          districtData: _json,
        });
        that.setData({
          "selectAddressData[2].name": _json[that.data.pickerValue[2]].areaname,
          "selectAddressData[2].id": _json[that.data.pickerValue[2]].id,
          "selectAddressData[2].index": that.data.pickerValue[2],
        });
      },
    });
  },
  _closeDateLw: function () {
    this.setData({
      openDateLw: false,
    });
  },
  _okAddress: function () {
    let seleData = this.data.selectAddressData;
    this.setData({
      district_id:seleData[2].id,
      city_id:seleData[1].id,
      province_id:seleData[0].id,
      "form.streetName": "",
      districtVal: "",
      addressName: seleData[0].name + " " + seleData[1].name + " " + seleData[2].name,
      "form.province": seleData[0].name,
      "form.city": seleData[1].name,
      "form.district": seleData[2].name,
    });
    this._getStreet(seleData[2].id);
    this._closeDateLw();
  },
  //获取街道
  _getStreet(param) {
    var that = this;
    let urlStr = app.getNetAddresss("member.memberAddress.street");
    urlStr += "&district_id=" + param;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1 && !app._isTextEmpty(res.data)) {
          if (!app._isTextEmpty(res.data) && res.data.length > 0) {
            that.setData({
              districtVal: res.data,
              isShowStreet: true,
            });
          } else {
            that.setData({
              isShowStreet: false,
            });
          }
        } else {
          that.setData({
            isShowStreet: false,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  submit_anew(){
    Dialog.confirm({
      title:"提示",
      message: '是否确定重新发布?',
    })
      .then(() => {
        this.submit();
      })
      .catch(() => {
        // on cancel
      });
  },
  submit() {
    app._postNetWork({
      url: app.getNetAddresss('plugin.be-within-call.frontend.role-apply.index'),
      data: {
        data: {
          identity: 1,
          realname: this.data.realname,
          mobile: this.data.mobile,
          province_id: this.data.province_id,
          city_id: this.data.city_id,
          district_id: this.data.district_id,
          street_id: this.data.street_id,
          detail_info: {
            age: this.data.age
          },
          annex_file: {
            id_card_front: this.data.id_card_front_img,
            id_card_reverse: this.data.id_card_reverse_img,
          }

        }
      },
      success:resdata=>{
        let {result,data,msg}= resdata.data;
        app.tips(msg);
        if(result){
          setTimeout(()=>{
            wx.navigateTo({
              url: '/packageJ/anytime_service/my/index/index',
            })
          },300)

        }
      }
    })
  },
  removeImg() {
    this.setData({
      videoUrl: null
    })
  },

  delete(e) {
    let field = e.currentTarget.dataset.field;
    this.setData({
      [field]: []
    })
  },
  onInput(e) {
    let field = e.currentTarget.dataset.field;
    let value = e.detail;
    this.setData({
      [field]: value
    })
  },
  afterRead(event) {
    wx.showLoading({
      title: '上传中'
    })
    const {
      file
    } = event.detail;
    let {
      field
    } = event.currentTarget.dataset;
    let urlStr = app.getNetAddresss("upload.uploadPic");
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      success: (resdata) => {
        app.tips("上传成功")
        let res = JSON.parse(resdata.data);
        let arr = []
        arr.push({
          ...file,
          url: res.data.img_url,
          status: "done",
          message: '上传完成'
        });
        this.setData({
          [field]: arr
        });
        if (field == 'id_card_taill') {
          this.setData({
            id_card_reverse_img: res.data.img
          })
        }
        if(field == 'id_card_front') {
          this.setData({
            id_card_front_img: res.data.img
          })

        }
        wx.hideLoading();
      },
      fail(err) {
        app.tips("上传失败")
        console.log(err);
        wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.validate();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})